﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.service.impl;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.project.entity.SearchProjectCv;
import com.hh.bss.project.service.SearchProjectService;
import com.hh.bss.talent.dao.CvContactDetailDao;
import com.hh.bss.talent.dao.CvDao;
import com.hh.bss.talent.dao.CvSalaryDao;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.entity.CvContactDetail;
import com.hh.bss.talent.entity.CvSalary;
import com.hh.bss.util.*;




@Service
@Transactional
public class CompanyCcServiceImpl extends BaseBusinessService<CompanyCc,java.lang.Integer>  implements CompanyCcService<CompanyCc,java.lang.Integer>{

	private CompanyCcDao companyCcDao;
	private CompanyCvDao companyCvDao;
	private CvDao cvDao;
	private CvSalaryDao cvSalaryDao;
	private CvContactDetailDao cvContactDetailDao;
	private SearchProjectService searchProjectService;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCompanyCcDaoImpl(CompanyCcDao dao) {
		this.companyCcDao = dao;
	}
	public void setCompanyCvDaoImpl(CompanyCvDao dao) {
		this.companyCvDao = dao;
	}
	public void setCvSalaryDaoImpl(CvSalaryDao dao) {
		this.cvSalaryDao = dao;
	}
	public void setCvDaoImpl(CvDao dao) {
		this.cvDao = dao;
	}
	public void setCvContactDetailDaoImpl(CvContactDetailDao dao) {
		this.cvContactDetailDao = dao;
	}
	public void setSearchProjectServiceImpl(SearchProjectService service) {
		this.searchProjectService = service;
	}
	public EntityDao getEntityDao() {
		return this.companyCcDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CompanyCcQuery query) {
		return companyCcDao.findPage(query);
	}
	public Page findProjectCcCvPage(CompanyCcQuery query) {
		return companyCcDao.findProjectCcCvPage(query);
	}
	public void save(CompanyCc entity) throws DataAccessException{
		getEntityDao().save(entity);
		//处理cv
	}
	public void saveCcCv(CompanyCc companyCc,Cv cv,List cvContacts){
		//保存Cv
		if(!StringHelper.isEmpty(cv.getCvName())){
			cvDao.save(cv);
			cvDao.flush();
			for(int i=0;cvContacts!=null&&i<cvContacts.size();i++){
				CvContactDetail cvContactDetail=(CvContactDetail)cvContacts.get(i);
				cvContactDetail.setCvId(cv.getId());
				cvContactDetailDao.save(cvContactDetail);
				cvContactDetailDao.flush();
			}
			//简历加入项目
			SearchProjectCv searchProjectCv=new SearchProjectCv();
			searchProjectCv.setCreateDt(new Date());
			searchProjectCv.setCreator(companyCc.getCreator());
			searchProjectCv.setProjectId(companyCc.getProjectId());
			searchProjectCv.setCvId(cv.getId());
			searchProjectService.cvJoinProject(searchProjectCv);
			cvContactDetailDao.flush();
			//
			
			//人选加入
			CompanyCv companyCv=new CompanyCv();
			companyCv.setCompanyId(companyCc.getCompanyId());
			companyCv.setDeptId(companyCc.getDeptId());
			companyCv.setTitleId(companyCc.getTitleId());
			companyCv.setCreateDt(new Date());
			companyCv.setCreator(companyCc.getCreator());
			companyCvDao.save(companyCv);
			companyCvDao.flush();
			
			if (!StringHelper.isEmpty(companyCc.getSalary())){				
				CvSalary cvSalary=new CvSalary();
				cvSalary.setCvId(cv.getId());
				cvSalary.setCurrency("RMB");
				cvSalary.setCurrentSalary(StringHelper.parseInt(companyCc.getSalary(), 0));
				cvSalary.setExpectSalary(0);
				cvSalary.setCreateDt(new Date());
				cvSalary.setCreator(cv.getCreator());			
			    cvSalaryDao.save(cvSalary);
			    cvSalaryDao.flush();
			}
		}
		
			
		//保存联系记录
		//保存CC
		companyCc.setRefCvid(cv.getId());
		companyCcDao.save(companyCc);
			
		
	}
	public Page findCcPage(CompanyCcQuery query) {
		// TODO Auto-generated method stub
		return companyCcDao.findCcPage(query);
	}
	
}
